package controllers

import (
	"encoding/json"
	"github.com/astaxie/beego/orm"
	"wego/models"
)

type AdminController struct {
	BaseController
}

func (this *AdminController) AdminList() {
	params := models.BaseQueryParam{1, 10}
	_ = json.Unmarshal(this.Ctx.Input.RequestBody, &params)
	list, count := models.AdminList(&params)
	result := models.ResultList{
		List:  list,
		Count: count,
	}
	this.JsonResult(1, "操作成功", &result)
}

//获取当前用户组成员
func (this *AdminController) GetAdminByGroupId() {
	m := models.GetAdminParams{}
	_ = json.Unmarshal(this.Ctx.Input.RequestBody, &m)
	list, count := models.GetAdminByGroupId(&m)
	result := models.ResultList{
		List:  list,
		Count: count,
	}
	this.JsonResult(1, "操作成功", &result)
}

func (this *AdminController) AdminSave() {
	m := models.Admin{}
	_ = json.Unmarshal(this.Ctx.Input.RequestBody, &m)

	tmp := models.Admin{Username: m.Username}
	o := orm.NewOrm()
	er := o.Read(&tmp, "username")
	if er == nil && tmp.Id != m.Id {
		this.JsonResult(0, "用户重复", "")
	}
	err := models.AdminSave(&m)
	if err == nil {
		this.JsonResult(1, "操作成功", &m)
	} else {
		this.JsonResult(0, "操作失败", "")
	}
}

func (this *AdminController) AdminStatus() {
	m := models.Admin{}
	_ = json.Unmarshal(this.Ctx.Input.RequestBody, &m)
	if m.Id == 0 {
		this.JsonResult(0, "参数错误", "")
	}
	err := models.AdminStatus(&m)
	if err == nil {
		this.JsonResult(1, "操作成功", &m)
	} else {
		this.JsonResult(0, "操作失败", "")
	}
}

func (this *AdminController) AdminDel() {
	m := models.Admin{}
	_ = json.Unmarshal(this.Ctx.Input.RequestBody, &m)
	if m.Id == 0 {
		this.JsonResult(0, "参数错误", "")
	}
	err := models.AdminDel(&m)
	if err == nil {
		this.JsonResult(1, "操作成功", &m)
	} else {
		this.JsonResult(0, "操作失败", "")
	}
}
